/*
 ============================================================================
 Name        : add_phase.c
 Author      : chenf
 Version     : 1.0
 Copyright   : loongsec
 Description : add phase in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "cgi.h"
#include "cookie.h"

int main(void) {
        sqlite3 *db = NULL;
        int  nrow=0,ncolumn=0;
        char **azResult = NULL;
        char *zErrMsg =0;
        char sql[1000];
        char *input;
        char *req_method;
        int  rc;

        if(isvalid_cookie()!=0){
            return 0;
        }

        rc = sqlite3_open(PATH_DATABASE,&db);

        printf("Content-type:text/json;charset=utf-8\n\n");

        if (rc) {
            fprintf(stderr,"Can't open database:%s\n",sqlite3_errmsg(db));
            sqlite3_close(db);
            return 0;

        } else {
            req_method = getenv("REQUEST_METHOD");

            FILE* fp = (FILE*) stdin;
            input = getcgidata(fp, req_method);

            //PARAM un = getParam(input, "username");
            //printf("Your find is %s = %s  \n", un.key, un.value );

            //printf("Your input is %s<br>  \n", input );
            PARAM method  = getParam(input, "method");

            PARAM stage_timing_index  = getParam(input, "stageTimingIndex");
            PARAM stage_index         = getParam(input, "stageIndex");
            PARAM pass_phase          = getParam(input, "passPhase");
            PARAM stage_green_time    = getParam(input, "stageGreenTime");
            PARAM stage_yellow_time   = getParam(input, "stageYellowTime");
            PARAM stage_red_time      = getParam(input, "stageRedTime");
            PARAM stage_option_parm   = getParam(input, "stageOptionParm");

            char last_modified[]      = {"datetime(CURRENT_TIMESTAMP,'localtime')"};

            if( strcmp(method.value, "add") ==0 ){
                sprintf(sql, " INSERT INTO [tb_stage_timing] (\
                    [stage_timing_index], \
                    [stage_index],\
                    [pass_phase], \
                    [stage_green_time], \
                    [stage_yellow_time],\
                    [stage_red_time],   \
                    [stage_option_parm],\
                    [last_modified])    \
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s);",
                    stage_timing_index.value,
                    stage_index.value,
                    pass_phase.value,
                    stage_green_time.value,
                    stage_yellow_time.value,
                    stage_red_time.value,
                    stage_option_parm.value,
                    last_modified);

            } else if( strcmp(method.value, "update") ==0 ){
                sprintf(sql, " UPDATE [tb_stage_timing]  \
                    set [pass_phase]           = %s,\
                      [stage_green_time]     = %s,\
                      [stage_yellow_time]    = %s,\
                      [stage_red_time]       = %s,\
                      [stage_option_parm]    = %s,\
                      [last_modified]        = %s \
                    WHERE [stage_timing_index] = %s \
                      AND [stage_index]        = %s ;",
                    pass_phase.value,
                    stage_green_time.value,
                    stage_yellow_time.value,
                    stage_red_time.value,
                    stage_option_parm.value,
                    last_modified,
                    stage_timing_index.value,
                    stage_index.value);

            } else if( strcmp(method.value, "delete") ==0 ){
                sprintf(sql, " DELETE FROM [tb_stage_timing]  \
                    WHERE [stage_timing_index]  = %s \
                     AND  [stage_index]         = %s;",
                    stage_timing_index.value,
                    stage_index.value);

            }
            //printf("SQL: %s", sql);

            int sr = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
            printf("{\"res\":\"%d\",\"msg\":\"%s\"}", sr, sqlite3_errmsg(db));

            sqlite3_close(db);//关闭数据库
            return 0;

        }
        fflush(stdout);

    return EXIT_SUCCESS;
}



